package demo520;

import java.util.*;


    public class TestMap {
        public static void main(String[] args) {
            EmployeeSystem system = new EmployeeSystem();
            system.start();
        }
    }

    class EmployeeSystem {
        private final Map<String, Emp> employeeMap = new HashMap<>();
        private final Scanner scanner = new Scanner(System.in);

        public void start() {
            while (true) {
                printMenu();
                int choice = scanner.nextInt();
                scanner.nextLine();  // 消耗换行符

                switch (choice) {
                    case 0:
                        System.out.println("系统已退出");
                        return;
                    case 1:
                        addEmployee();
                        break;
                    case 2:
                        showAllEmployees();
                        break;
                    case 3:
                        findEmployeeByName();
                        break;
                    case 4:
                        deleteEmployee();
                        break;
                    case 5:
                        findEmployeeById();
                        break;
                    case 6:
                        updateEmployeeAge();
                        break;
                    default:
                        System.out.println("无效的选项，请重新输入");
                }
            }
        }

        private void printMenu() {
            System.out.println("\n=== 员工信息管理系统 ===");
            System.out.println("0. 退出系统");
            System.out.println("1. 添加员工");
            System.out.println("2. 显示所有员工");
            System.out.println("3. 根据姓名查询");
            System.out.println("4. 删除员工");
            System.out.println("5. 根据编号查询");
            System.out.println("6. 修改员工年龄");
            System.out.print("请选择操作：");
        }

        private void addEmployee() {
            System.out.print("输入员工编号：");
            String id = scanner.nextLine();

            if (employeeMap.containsKey(id)) {
                System.out.println("该员工编号已存在！");
                return;
            }

            System.out.print("输入员工姓名：");
            String name = scanner.nextLine();

            System.out.print("输入员工年龄：");
            int age = scanner.nextInt();
            scanner.nextLine();  // 消耗换行符

            employeeMap.put(id, new Emp(id, name, age));
            System.out.println("员工添加成功！");
        }

        private void showAllEmployees() {
            if (employeeMap.isEmpty()) {
                System.out.println("当前没有员工记录");
                return;
            }

            System.out.println("\n=== 所有员工列表 ===");
            for (Emp emp : employeeMap.values()) {
                System.out.println(emp);
            }
        }

        private void findEmployeeByName() {
            System.out.print("输入查询姓名：");
            String name = scanner.nextLine();

            List<Emp> results = new ArrayList<>();
            for (Emp emp : employeeMap.values()) {
                if (emp.getEmpName().equalsIgnoreCase(name)) {
                    results.add(emp);
                }
            }

            if (results.isEmpty()) {
                System.out.println("未找到匹配员工");
            } else {
                System.out.println("找到 " + results.size() + " 条结果：");
                results.forEach(System.out::println);
            }
        }

        private void deleteEmployee() {
            System.out.print("输入要删除的员工编号：");
            String id = scanner.nextLine();

            if (employeeMap.remove(id) != null) {
                System.out.println("员工删除成功");
            } else {
                System.out.println("未找到该员工");
            }
        }

        private void findEmployeeById() {
            System.out.print("输入查询编号：");
            String id = scanner.nextLine();

            Emp emp = employeeMap.get(id);
            if (emp != null) {
                System.out.println("查询结果：\n" + emp);
            } else {
                System.out.println("未找到该员工");
            }
        }

        private void updateEmployeeAge() {
            System.out.print("输入要修改的员工编号：");
            String id = scanner.nextLine();

            Emp emp = employeeMap.get(id);
            if (emp == null) {
                System.out.println("员工不存在");
                return;
            }

            System.out.print("输入新的年龄：");
            int newAge = scanner.nextInt();
            scanner.nextLine();  // 消耗换行符

            emp.setAge(newAge);
            System.out.println("年龄修改成功");
        }
    }

    class Emp {
        private final String empNumber;
        private final String empName;
        private int age;

        public Emp(String empNumber, String empName, int age) {
            this.empNumber = empNumber;
            this.empName = empName;
            this.age = age;
        }

        // Getter方法
        public String getEmpNumber() {
            return empNumber;
        }

        public String getEmpName() {
            return empName;
        }

        public int getAge() {
            return age;
        }

        // Setter方法
        public void setAge(int age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return String.format("编号: %-6s 姓名: %-8s 年龄: %-3d",
                    empNumber, empName, age);
        }
    }
